package com.coder520.mamabike.service.impl;

import com.coder520.mamabike.common.ServerResponse;
import com.coder520.mamabike.pojo.BikeInfo;
import com.coder520.mamabike.service.IBikeService;
import com.google.common.collect.Lists;
import com.mongodb.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by ASUS on 2017/8/21.
 */
@Service("iBikeService")
public class BikeServiceImpl implements IBikeService {

    private static final String LOCATION = "location";

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     *
     * @param coordinate 坐标
     * @param count 查询数量
     * @param distance 查询范围   米
     * @return
     */
    public ServerResponse findRoundBike(String coordinate,Integer count,Integer distance) {
        String[] coors = coordinate.split(",");
        Double x = Double.parseDouble(coors[0]);
        Double y = Double.parseDouble(coors[1]);
        if (x != null && y != null) {
            DBObject dbObject = new BasicDBObject();
            dbObject.put(LOCATION,
                    new BasicDBObject("$nearSphere",
                            new BasicDBObject("$geometry",
                                    new BasicDBObject("type", "Point")
                                            .append("coordinates", new double[]{x, y}))
                                    .append("$maxDistance", distance)

                    ));

            dbObject.put("status",1);

            List<DBObject> dbObjectList = mongoTemplate.getCollection("bike").find(dbObject).limit(count).toArray();

            List<BikeInfo> bikeInfoList = Lists.newArrayList();

            for (DBObject db:dbObjectList){

                BikeInfo bikeInfo = new BikeInfo();
                bikeInfo.setNumber(((Integer)db.get("bike_no")).longValue());

                BasicDBList basicDBList = (BasicDBList) (((BasicDBObject) (db.get(LOCATION))).get("coordinates"));
                Double[] temp = new Double[2];
                basicDBList.toArray(temp);
                bikeInfo.setCoordinate(temp);
                bikeInfo.setStatus((Integer) db.get("status"));
                bikeInfoList.add(bikeInfo);
            }
            return ServerResponse.createBySuccess(bikeInfoList);
        }
        return ServerResponse.createByError("参数错误");
    }

    public ServerResponse findRoundBikeGetDistance(String coordinate,Integer count,Integer distance) {

        String[] coors = coordinate.split(",");
        Double x = Double.parseDouble(coors[0]);
        Double y = Double.parseDouble(coors[1]);
        if (x != null && y != null) {
            List<DBObject> dbObjectList = Lists.newArrayList();
            DBObject dbObject = new BasicDBObject();
            dbObject.put("$geoNear",
                    new BasicDBObject("near",
                            new BasicDBObject("type","Point").append("coordinates",new double[]{x,y}))
                                .append("distanceField","distance")
                                .append("num",count==null?10:count)
                                .append("maxDistance",distance==null?500:distance)
                                .append("spherical",true)
                                .append("query",new BasicDBObject("status",1))


            );

            dbObjectList.add(dbObject);


            Cursor cursor = mongoTemplate.getCollection("bike").aggregate(dbObjectList, AggregationOptions.builder().build());

            List<BikeInfo> result = Lists.newArrayList();

            while (cursor.hasNext()){
                DBObject object = cursor.next();

                BikeInfo bikeInfo = new BikeInfo();

                bikeInfo.setNumber(((Integer)object.get("bike_no")).longValue());
                bikeInfo.setStatus((Integer) object.get("status"));
                BasicDBList basicDBList = (BasicDBList) (((BasicDBObject) (object.get(LOCATION))).get("coordinates"));
                Double[] temp = new Double[2];
                basicDBList.toArray(temp);
                bikeInfo.setCoordinate(temp);
                bikeInfo.setDistance((Double) object.get("distance"));

                result.add(bikeInfo);
            }
            return ServerResponse.createBySuccess(result);
        }
        return ServerResponse.createByError("参数错误");
    }


}
